<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<%
  String path = request.getContextPath();
%>
<jsp:include page="/pages/commons/top.jsp" />
<jsp:include page="/pages/commons/left.jsp" />

<div data-options="region:'center',title:'供应商管理',iconCls:'icon-ok'">  
	<table id="dg" title="供应商列表" class="easyui-datagrid" style="width: 1180px; height: 447px" toolbar="#toolbar" fitColumns="true" method="post" singleSelect="false" loadMsg="数据载入中...">
		<thead>
			<tr>
			    <th data-options="field:'ck',checkbox:true"></th> 
				<th field="id" width="30">流水号</th>
				<th field="name" width="50">供应商名称</th>
				<th field="parentIdName" width="50">父级供应商</th>
				<th field="cooperateLevelName" width="50">合作等级</th>
				<th field="contactPerson" width="50">联系人</th>
				<th field="address" width="50">地址</th>
				<th field="phone" width="50">电话</th>
				<th field="mobile" width="50">手机</th>
				<th field="qualification" width="50">资质</th>
				<th field="businessScope" width="50">经营范围</th>
				<th field="createTimeStr" width="50">创建时间</th>
				<th field="modifyTimeStr" width="50">修改时间</th>
				<th field="note" width="50">备注</th>
			</tr>
		</thead>
	</table>
	<div id="toolbar" style="padding:5px;height:auto">  
        <div style="margin-bottom:5px">  
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newSupplier()">添加供应商</a> 
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editSupplier()">修改供应商</a> 
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="delSupplier()">删除供应商</a>
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" plain="true" onclick="showSupplier()">查看供应商</a>
		</div>
		<div> 
		    <label>供应商名称：</label> <input id="queryNameObj" name="queryName" class="easyui-validatebox"/>
		    <label>合作等级：</label> <input id="queryCooperateLevelObj" name="queryCooperateLevel" url="<%=path%>/dictionary/getList.do?dictType=COOPERATE_LEVEL" class="easyui-combobox" valueField="dictValue" textField="dictName"/>
		    <label>联系人：</label> <input id="queryContactPersonObj" name="queryContactPerson" class="easyui-validatebox"/> 
            <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="querySupplier()">查询</a>  
        </div>
	</div>
	<div id="page" class="easyui-pagination" style="border:1px solid #ddd;width: 1180px"></div>

	<div id="dlg" class="easyui-dialog" style="width: 1000px; height: 300px; padding: 10px 20px" closed="true" buttons="#dlg-buttons">
		<div class="ftitle">供应商信息</div>
		<form id="fm" method="post" novalidate>
		    <input id="idObj" name="id" type="hidden"/>
		    <input id="parentIdObj" name="parentId" type="hidden"/>
		    <div class="fitem">
				<label>供应商名称:</label><input id="nameObj" name="name" class="easyui-validatebox" required="true" validType="length[0,100]"/>
				<label>父级供应商:</label><input id="parentIdNameObj" name="parentIdName" class="easyui-validatebox" disabled="true"/>
				<label>合作等级:</label><input id="cooperateLevelObj" name="cooperateLevel" class="easyui-combobox" url="<%=path%>/dictionary/getList.do?dictType=COOPERATE_LEVEL" required="true" valueField="dictValue" textField="dictName"/>

			    <label>联系人:</label><input id="contactPersonObj" name="contactPerson" class="easyui-validatebox" validType="length[0,30]"/>
				<label>座机:</label><input id="phoneObj" name="phone" class="easyui-validatebox" validType="length[0,30]"/>
				<label>手机:</label><input id="mobileObj" name="mobile" class="easyui-validatebox" validType="length[0,30]"/>

			    <label>创建时间:</label><input id="createTimeObj" name="createTimeStr" class="easyui-validatebox" disabled="true"/>
				<label>修改时间:</label><input id="modifyTimeObj" name="modifyTimeStr" class="easyui-validatebox" disabled="true"/>

				<label>地址:</label><input id="addressObj" style="width:770px" class="easyui-validatebox" name="address" validType="length[0,200]"/>
				<label>资质:</label><input id="qualificationObj" style="width:770px" class="easyui-validatebox" name="qualification" validType="length[0,500]"/>
				<label>经营范围:</label><input id="businessScopeObj" style="width:770px" class="easyui-validatebox" name="businessScope" validType="length[0,1000]"/>
				<label>备注:</label><input id="noteObj" style="width:770px" class="easyui-validatebox" name="note" validType="length[0,1000]"/>
			</div>

		</form>
	</div>
	<div id="dlg-buttons">
		<a id="saveButton" href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveSupplier()">保存</a> 
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
	</div>
</div>

<jsp:include page="/pages/commons/footer.jsp" />


<script type="text/javascript">  
        var url;  
        function newSupplier(){  
            $('#dlg').dialog('open').dialog('setTitle','添加供应商'); 
            $('#saveButton').linkbutton('enable'); 
            $('#fm').form('clear'); 
            url = "<%=path%>/supplier/add.do";  
            addOption(null);
			$("#parentIdNameObj").val('');
            $("#parentIdObj").val('0');
			var row = $('#dg').datagrid('getSelected'); 
            if (row){
                $("#parentIdNameObj").val(row.name);
                $("#parentIdObj").val(row.id);
			}
        } 

        function addOption(row){
			if(row != null){
			    $("#cooperateLevelObj").combobox("select",row.cooperateLevel);
			}
        }		
        

        function editSupplier(){  
            var row = $('#dg').datagrid('getSelected');  
            $('#saveButton').linkbutton('enable'); 
            if (row){  
                $('#dlg').dialog('open').dialog('setTitle','修改供应商');  
                $('#fm').form('load',row);
                url = "<%=path%>/supplier/mod.do";  
                addOption(row);
            }  
        } 
        
        function showSupplier(){
        	var row = $('#dg').datagrid('getSelected');  
            if (row){
            	$('#dlg').dialog('open').dialog('setTitle','供应商信息');  
                $('#fm').form('load',row);
                $('#saveButton').linkbutton('disable'); 
            }
        }
        
        function saveSupplier(){ 
			if($('#fm').form('validate')){
				var data = "";
				data += "name="+$("#nameObj").val();
			    data += "&parentId="+$("#parentIdObj").val();
				data += "&cooperateLevel="+$("#cooperateLevelObj").combobox("getValue");
				
				if($("#idObj").val()!=''){
				    data += "&id="+$("#idObj").val();
				}
				if($("#contactPersonObj").val()!=''){
					data += "&contactPerson="+$("#contactPersonObj").val();
				}
				if($("#qualificationObj").val()!=''){
					data += "&qualification="+$("#qualificationObj").val();
				}
                if($("#noteObj").val()!=''){
                	data += "&note="+$("#noteObj").val();
				}

                if($("#addressObj").val()!=''){
                	data += "&address="+$("#addressObj").val();
				}
                if($("#phoneObj").val()!=''){
                	data += "&phone="+$("#phoneObj").val();
				}
                if($("#mobileObj").val()!=''){
                	data += "&mobile="+$("#mobileObj").val();
				}

                if($("#businessScopeObj").val()!=''){
                	data += "&businessScope="+$("#businessScopeObj").val();
				}

			    $.post(url,data,saveCallBack);  
			}
        } 

        function saveCallBack(result){
            var results = result.split("|"); 
	        if(results[0] == "error"){
	            $.messager.alert('错误',getErrorInfo(results[1]),'error'); 
	            return false;
            }else if(results[0] == "success"){
	            $.messager.alert('成功',getErrorInfo(results[1]),'info'); 
	            $('#dlg').dialog('close');      // close the dialog  
                $('#dg').datagrid('reload');
				initPage("");
    	        return true;
            } 
        }	
 
        function delSupplier(){  
            var row = $('#dg').datagrid('getSelections');  
            if (row.length > 0){  
                $.messager.confirm('Confirm','确定删除该供应商吗?',function(r){  
                    if (r){  
						var data = "";
						for(var i=0;i<row.length;i++){
						    data += row[i].id
						    if(i<row.length - 1){
						        data+=",";
							}
						}
                    	$.post("<%=path%>/supplier/del.do",{'ids':data},delCallBack);   
                    }  
                });  
            }  
        } 
        
        function delCallBack(result){
        	var results = result.split("|"); 
	        if(results[0] == "error"){
	            $.messager.alert('错误',getErrorInfo(results[1]),'error'); 
	            return false;
	        }else if(results[0] == "success"){
	            $.messager.alert('成功',getErrorInfo(results[1]),'info'); 
	            $('#dg').datagrid('reload');
	            var data = "?name="+$("#queryNameObj").val();
	        	data += "&cooperateLevel="+$("#queryCooperateLevelObj").combobox('getValue');
	        	data += "&contactPerson="+$("#queryContactPersonObj").val();
				data += "&parentId=-1";
				initPage(data);
    	        return true;
            } 
        } 
		
		$(function(){ 
		   var data = "?parentId=-1"; 
　　                              initPage(data);
           initDataGrid();
　　                   }); 

        function initDataGrid(){
		    var pageSize = $('#page').pagination('options').pageSize;
			var pageNum = $('#page').pagination('options').pageNumber;
			var startNum = pageSize*(pageNum-1);
			var queryData = "?startNum="+startNum+"&limit="+pageSize+"&parentId=-1";
		    var queryUrl = "<%=path %>/supplier/getListForPage.do"+queryData;
        	$('#dg').datagrid({url:queryUrl});
        	
		}
        
        function initPage(data){
        	$.post("<%=path%>/supplier/getTotalCount.do"+data,null,function(result){
        		$('#page').pagination({total:result});
            });
        }
        
        
        function querySupplier(){
        	var data = "?name="+$("#queryNameObj").val();
        	data += "&cooperateLevel="+$("#queryCooperateLevelObj").combobox('getValue');
        	data += "&contactPerson="+$("#queryContactPersonObj").val();
			data += "&parentId=-1";
			initPage(data);
			var pageSize = $('#page').pagination('options').pageSize;
			var pageNum = $('#page').pagination('options').pageNumber;
			var startNum = pageSize*(pageNum-1);
        	var queryData = data + "&startNum="+startNum+"&limit="+pageSize;
        	var queryUrl = "<%=path %>/supplier/getListForPage.do"+queryData;
        	$('#dg').datagrid({url:queryUrl});
        }
        
        
        $('#page').pagination({
        	onSelectPage:function(pageNumber, pageSize){
        		querySupplier();
        	},
        	onRefresh:function(pageNumber, pageSize){
        		querySupplier();
        	},
        	onChangePageSize:function(pageSize){
        		querySupplier();
        	}
        });

    </script>
<style type="text/css">
#fm {
	margin: 0;
	padding: 10px 30px;
}

.ftitle {
	font-size: 14px;
	font-weight: bold;
	padding: 5px 0;
	margin-bottom: 10px;
	border-bottom: 1px solid #ccc;
}

.fitem {
	margin-bottom: 5px;
}

.fitem label {
	display: inline-block;
	width: 80px;
}
</style>

</html>
